package com.getpebble.android.common.framework.install.app;

import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.install.PebbleBundle;
import com.getpebble.android.common.framework.install.PutBytesType;
import com.getpebble.android.common.model.AppInfo;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class AppBundle extends PebbleBundle<AppManifest> {
    public static final String TAG = AppBundle.class.getSimpleName();
    private AppInfo mAppInfo;
    private List<PutBytesType> mInstallableComponents;
    private AppManifest mManifest = ((AppManifest) parseManifest(AppManifest.class));

    public enum Platform {
        APLITE("", "aplite"),
        BASALT("basalt/", "basalt"),
        CHALK("chalk/", "chalk");
        
        private String code;
        private final String path;

        private Platform(String path, String code) {
            this.path = path;
            this.code = code;
        }
    }

    public AppBundle(ZipFile file, Map<String, ZipEntry> components) throws IOException, JsonSyntaxException {
        super(file, components);
        if (this.mManifest == null) {
            throw new IllegalStateException("'Manifest' cannot be null!");
        }
        parseAppInfo();
    }

    private void parseAppInfo() throws IOException, JsonSyntaxException {
        if (getComponents().containsKey("appinfo.json")) {
            InputStream inputStream = getInputStreamForComponent("appinfo.json");
            this.mAppInfo = (AppInfo) new Gson().fromJson(new BufferedReader(new InputStreamReader(inputStream)), AppInfo.class);
            inputStream.close();
            return;
        }
        Trace.error(TAG, "No app info found.");
        throw new IOException("Components do not contain app info");
    }

    public List<PutBytesType> getInstallableComponents() {
        if (this.mInstallableComponents == null) {
            this.mInstallableComponents = new ArrayList();
            if (hasResources()) {
                this.mInstallableComponents.add(PutBytesType.APP_RESOURCES);
            }
            if (hasWorker()) {
                this.mInstallableComponents.add(PutBytesType.WORKER);
            }
            this.mInstallableComponents.add(PutBytesType.APP);
        }
        return this.mInstallableComponents;
    }

    public boolean hasJsFile() {
        return getComponents().containsKey("pebble-js-app.js");
    }

    public boolean hasWorker() {
        return getManifest().hasWorker();
    }

    public AppManifest getManifest() {
        return this.mManifest;
    }

    public AppInfo getAppInfo() {
        return this.mAppInfo;
    }
}
